<?php
/**
 * 队列开通
 */

set_time_limit(0);

require_once(dirname(__FILE__) . '/../config/saasapi.php');

define('SASS_APP_KEY', 'taoguan');
define('SAAS_SECRE_KEY', '49F4589687E79D815339B13A73E5FBB4');

function fetchActionFromQueue($code) {

	$api = new SaasOpenClient();
	$api->appkey = SASS_APP_KEY;
	$api->secretKey = SAAS_SECRE_KEY;
	$api->format = 'json';

	$params = array('service_code' => $code);
	$result = $api->execute('host.get_pending', $params);

	unset($api);
	if ($result->success == 'true') {
		if ($result->data == 'QUEUE_END') {
			return null;
		} else {
			return $result->data;
		}
	} else {
		return null;
	}
}

function callActiveProcess($info) {
	
	$serverName = $info->server_name;
	$orderId = $info->order_id;
	$hostId = $info->host_id;

	$cmd = sprintf("/usr/local/php/bin/php /data/httpd/tg.taoex.com/script/initDomain.php %s %s %s", $serverName, $orderId, $hostId);
	exec($cmd ,$b, $a);
	//print_r($out);
}

while(true) {

	$info = fetchActionFromQueue('taoex-tg');
	
	if (!empty($info) && !empty($info->server_name)) {
		
		echo 'Active ' . $info->server_name . "\n";
		callActiveProcess($info);
	}

	
	//usleep(10000);
	sleep(1);
}
